﻿namespace Common
{
    using System;
    using System.Globalization;
    using System.Xml;

    public class Xml
    {
        public static XmlDocument document;

        public static XmlElement AddElement(XmlNode parent, string name)
        {
            return AddElement(parent, name, "");
        }

        public static XmlElement AddElement(XmlNode parent, string name, double value)
        {
            return AddElement(parent, name, value.ToString(DoubleFormat));
        }

        public static XmlElement AddElement(XmlNode parent, string name, string text)
        {
            var newChild = document.CreateElement(name);
            if (!string.IsNullOrEmpty(text))
            {
                newChild.InnerText = text;
            }
            parent.AppendChild(newChild);
            return newChild;
        }

        public static double GetDouble(XmlNode element)
        {
            try
            {
                return double.Parse(element.InnerText, DoubleFormat);
            }
            catch
            {
                return 0.0;
            }
        }

        public static IFormatProvider DoubleFormat
        {
            get
            {
                var info = new NumberFormatInfo {NumberDecimalSeparator = "."};
                return info;
            }
        }
    }
}

